//
//  UIFontTest.swift
//  EasySwift
//
//  Created by 王隆帅 on 2017/6/8.
//  Copyright © 2017年 王隆帅. All rights reserved.
//

import UIKit

class UIFontTest: UIViewController {
    
    var textStyleIndex = 0;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white;
        // Do any additional setup after loading the view.
        
        
//        self.testFontTextStyle();
//        self.testPreferredFontNotify();
//        self.testGetSystemFont()
        self.getAllSystemFonts();
        
    }
    
    // MARK: 测试获取系统内所有可用字体
    func getAllSystemFonts() {
        
        UIFont.familyNames.map {
            
            UIFont.fontNames(forFamilyName: $0);
            
            }.forEach { (fonts:[String]) in
                
                fonts.forEach({
                    
                    print($0);
                })
        };
        
    }
    
    // MARK: 测试获取系统字体
    func testGetSystemFont() {
        
        self.addFontLabelWithFont(font: UIFont.systemFont(ofSize: 18));
        self.addFontLabelWithFont(font: UIFont.boldSystemFont(ofSize: 18));
        self.addFontLabelWithFont(font: UIFont.italicSystemFont(ofSize: 18));
        self.addFontLabelWithFont(font: UIFont.systemFont(ofSize: 18, weight: 3));
        self.addFontLabelWithFont(font: UIFont.monospacedDigitSystemFont(ofSize: 18, weight: 3));
        
    }
    
    func addFontLabelWithFont(font: UIFont) {
        
        let label = UILabel.init(frame: CGRect.init(x: 40, y: 80 + self.textStyleIndex * 40, width: 400, height: 20));
        label.text = font.fontName;
        label.font = font;
        self.view.addSubview(label);
        
        self.textStyleIndex += 1;
    }
    
    // MARK: 测试 UITraitCollection
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        
        print(previousTraitCollection ?? "测试没有获取到TraitCollection");
        
        guard previousTraitCollection != nil else {
            
            return;
        }
        
        self.view.subviews.forEach { (view) in
            
            if view is UILabel {
                
                let label = view as! UILabel;
                let style = label.font.fontDescriptor.object(forKey: UIFontDescriptorTextStyleAttribute) as! String
                if #available(iOS 10.0, *) {
                    label.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle(rawValue: style), compatibleWith: previousTraitCollection)
                } else {
                    // Fallback on earlier versions
                };
            }
        };
    }
    
    // MARK: 测试 preferredFont 监听通知
    func testPreferredFontNotify() {
        
        NotificationCenter.default.addObserver(self, selector: #selector(resetLabelStyle(notification:)), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
    }
    
    // 回调通知
    func resetLabelStyle(notification: Notification) {
        
        self.view.subviews.forEach { (view) in
            
            if view is UILabel {
                
                let label = view as! UILabel;
                let style = label.font.fontDescriptor.object(forKey: UIFontDescriptorTextStyleAttribute) as! String
                label.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle(rawValue: style));
            }
        };
    }
    
    // MARK: 测试 preferredFont
    func testFontTextStyle() {
        
        self.addFontLabelWithTextStyle(style: .title1);
        self.addFontLabelWithTextStyle(style: .title2);
        self.addFontLabelWithTextStyle(style: .title3);
        self.addFontLabelWithTextStyle(style: .headline);
        self.addFontLabelWithTextStyle(style: .subheadline);
        self.addFontLabelWithTextStyle(style: .body);
        self.addFontLabelWithTextStyle(style: .callout);
        self.addFontLabelWithTextStyle(style: .footnote);
        self.addFontLabelWithTextStyle(style: .caption1);
        self.addFontLabelWithTextStyle(style: .caption2);
        
        // 为了表征Size Classes，Apple在iOS8中引入了一个新的类，UITraitCollection。这个类封装了像水平和竖直方向的Size Class等信息。
        // 返回与文本样式关联的字体的实例，并适当地对特性集合中定义的内容大小类别进行适当缩放。
        //        self.addFontLabelWithTraitCollection(traitCollection: UITraitCollection.init(userInterfaceIdiom: UIUserInterfaceIdiom.pad));
        //        self.addFontLabelWithTraitCollection(traitCollection: UITraitCollection.init(userInterfaceIdiom: UIUserInterfaceIdiom.phone));
        //        self.addFontLabelWithTraitCollection(traitCollection: UITraitCollection.init(userInterfaceIdiom: UIUserInterfaceIdiom.tv));
        //        self.addFontLabelWithTraitCollection(traitCollection: UITraitCollection.init(userInterfaceIdiom: UIUserInterfaceIdiom.carPlay));
    }
    
    // 添加label
    func addFontLabelWithTextStyle(style: UIFontTextStyle) {
        
        let label = UILabel.init(frame: CGRect.init(x: 40, y: 80 + self.textStyleIndex * 40, width: 400, height: 20));
        label.text = String(style.rawValue);
        label.font = UIFont.preferredFont(forTextStyle: style);
        label.adjustsFontForContentSizeCategory = true;
        self.view.addSubview(label);
        
        self.textStyleIndex += 1;
    }
    
    func addFontLabelWithTraitCollection(traitCollection: UITraitCollection) {
        
        let label = UILabel.init(frame: CGRect.init(x: 40, y: 80 + self.textStyleIndex * 40, width: 400, height: 20));
        label.text = String(describing: traitCollection.layoutDirection);
        label.font = UIFont.preferredFont(forTextStyle: .callout, compatibleWith: traitCollection);
        self.view.addSubview(label);
        
        self.textStyleIndex += 1;
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
